package com.haojii.helloworld;

import java.lang.reflect.Field;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Contact {

	@PrimaryKey
	@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
	private Long id;
	@Persistent
	private Date birthday;
	@Persistent
	private String firstname;
	@Persistent
	private String lastname;
	@Persistent
	private String email;
	@Persistent
	private String mobilephone;
	@Persistent
	private String gravatarURL;
	
	public Contact(Date birthday, String firstname, String lastname,
			String email, String mobilephone) {
		super();
		this.birthday = birthday;
		this.firstname = firstname;
		this.lastname = lastname;
		this.email = email;
		this.mobilephone = mobilephone;
		this.gravatarURL = MD5HashUtils.getGravatarURL(email);
	}
	public Contact() {
		super();
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public String getFirstname() {
		return firstname;
	}
	public void setFirstname(String firstname) {
		this.firstname = firstname;
	}
	public String getLastname() {
		return lastname;
	}
	public void setLastname(String lastname) {
		this.lastname = lastname;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getMobilephone() {
		return mobilephone;
	}
	public void setMobilephone(String mobilephone) {
		this.mobilephone = mobilephone;
	}
	@Override
	public String toString() {
		return "Contact " + "[id=" + id + ", birthday=" + birthday + ", email=" + email
				+ ", firstname=" + firstname + ", lastname="
				+ lastname + ", mobilephone=" + mobilephone + ", gravatarURL=" + gravatarURL + "]";
	}
	
	public String asJSON()
	{
		StringBuilder sb = new StringBuilder();
		try {
			sb.append("{");
			for (Field f : Contact.class.getDeclaredFields()) {
				if(!(f.getName().startsWith("jdo")))
				{
					sb.append("\"");
					sb.append(f.getName());
					sb.append("\":\"");
					if(f.getType()==Date.class)
					{
						SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
						String value="";
						try {
							value = sf.format(f.get(this));
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						}
						sb.append(value);
					}
					else 
					{
						sb.append(f.get(this));
					}
					sb.append("\",");
				}
			}
			sb.deleteCharAt(sb.length()-1);
			sb.append("}");
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		return sb.toString();
	}
	
	public String getGravatarURL() {
		return gravatarURL;
	}
	public void setGravatarURL(String gravatarURL) {
		this.gravatarURL = gravatarURL;
	}
	
    
}
